﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="EmbeddedInteger32.cs">
//   2004-2012 DHGMS Solutions. Some Rights Reserved. Licensed under GNU General Public License version 2 (GPLv2)
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.TplHelper.Controller.Task.Sql
{
    using System.Data.Common;
    using System.Reflection;

    using Dhgms.DataManager.Model.Helper.Database;
    using Dhgms.TplHelper.Model.Info.TaskResult;

    /// <summary>
    /// Base class for an SQL task that processes a 32-bit integer from an embedded script
    /// </summary>
    /// <typeparam name="TDatabaseHelperClass">
    /// The type for the database helper
    /// </typeparam>
    /// <typeparam name="TParameterClass">
    /// The type for the SQL Parameters
    /// </typeparam>
    public abstract class EmbeddedInteger32<TDatabaseHelperClass, TParameterClass> :
        Embedded<SingleResultInteger32, TDatabaseHelperClass, TParameterClass>
        where TDatabaseHelperClass : DatabaseHelperBase<TParameterClass>, new()
        where TParameterClass : DbParameter
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="EmbeddedInteger32{TDatabaseHelperClass,TParameterClass}"/> class. 
        /// </summary>
        /// <param name="taskName">
        /// </param>
        /// <param name="connectionString">
        /// </param>
        /// <param name="assembly">
        /// </param>
        /// <param name="resourceNamespace">
        /// </param>
        /// <param name="resourceFileName">
        /// </param>
        /// <param name="assumeSqlSafe">
        /// Whether to skip the SQL injection safety check.  Use only if you know the code you are passing in is safe and contains a quoted string that is preventing it passing the saftey check
        /// </param>
        protected EmbeddedInteger32(
            string taskName, 
            string connectionString, 
            Assembly assembly, 
            string resourceNamespace, 
            string resourceFileName, 
            bool assumeSqlSafe)
            : base(taskName, connectionString, assembly, resourceNamespace, resourceFileName, assumeSqlSafe)
        {
        }

        #endregion

        #region Methods

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// The result.
        /// </param>
        protected override void OnProcess(SingleResultInteger32 result)
        {
            result.Result = this.DbHelper.GetInteger32(
                this.ConnectionString, this.GetSql(), this.GetParameters(), this.AssumeSqlSafe);
        }

        #endregion
    }
}